//package com.xdxc.gateway.config;
//
//
//import com.xdxc.utils.JwtUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.Arrays;
//import java.util.List;
//
//@Component
//@Configuration
//public class WhitelistFilter implements GlobalFilter, Ordered {
//    @Autowired
//    StringRedisTemplate redisTemplate;
//    // 白名单列表（可以根据需求动态加载）
//    private static final List<String> WHITE_LIST = Arrays.asList(
//            "/api/public/**", // 公共接口
//            "/auth/login",    // 登录接口
//            "/health"         // 健康检查接口
//    );
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        String path = exchange.getRequest().getPath().value();
//        if (WHITE_LIST.stream().anyMatch(pattern -> path.matches(pattern.replace("**", ".*")))) {
//            return chain.filter(exchange); // 白名单路径放行
//        }
//
//        String token = exchange.getRequest().getHeaders().getFirst("Authorization");
//        if (token != null && token.startsWith("Bearer ")) {
//            token = token.substring(7);
//            try {
//                if (JwtUtil.validateToken(token, redisTemplate)) {
//                    return chain.filter(exchange);
//                }
//            } catch (Exception e) {
//                return chain.filter(exchange);
//            }
//        }
//
//        return chain.filter(exchange);
//    }
//    /**
//     * 判断请求路径是否在白名单中
//     */
//    private boolean isInWhitelist(String path) {
//        return WHITE_LIST.stream()
//                .anyMatch(pattern -> path.matches(pattern.replace("**", ".*")));
//    }
//
//    @Override
//    public int getOrder() {
//        return Ordered.HIGHEST_PRECEDENCE; // 设置优先级为最高
//    }
//}